/*
package com.thread.threadfirst.marvel.filter;

import com.fugui.marvel.common.utils.HttpUtils;
import com.fugui.marvel.service.common.dto.ReturnT;
import com.fugui.marvel.service.common.dto.user.SsoUser;
import com.fugui.marvel.sso.server.conf.Conf;
import com.fugui.marvel.sso.server.login.SsoTokenLoginHelper;
import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.exception.ZuulException;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;
import org.springframework.util.PatternMatchUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Enumeration;

*/
/**
 * 屏蔽非法访问请求，对于只限于服务间调用的api，请遵守命名规范：uri中含有-anon/internal
 *//*

@Component
public class InternalURIAccessFilter extends ZuulFilter implements InitializingBean, DisposableBean {

    private static Logger log = LoggerFactory.getLogger(InternalURIAccessFilter.class);

    @Autowired
    SsoTokenLoginHelper ssoTokenLoginHelper;

//            {"/m2.0/facade/", "*"},


*/
/*    {"/m2.0/facade/credit/getFristPageBankCardList"},
    {"/m2.0/facade/loan/getFristPageProductList"},
    {"/m2.0/facade/user/app/slideshow/query"},
    {"/m2.0/facade/user/news/getnewsby/brandidandclassification/andpage"},
    {"/m2.0/facade/user/thirdlevel/prod/list"},
    {"/m2.0/facade/user/thirdlevel/prod/help/buy"},*//*

    private static final String[][] preAuthenticationIgnoreUris = {
            {"/m2.0/facade/invite/example", "*"},
            {"/m2.0/facade/applogin", "*"},
            {"/m2.0/facade/applogout", "*"},
            {"/m2.0/facade/applogincheck", "*"},
            {"/m2.0/facade/v2/api-docs", "*"},
            {"/v2/api-docs", "*"},
            {"/m2.0/shield", "*"},
            {"/m2.0/facade/credit/getFristPageBankCardList", "*"},
            {"/m2.0/facade/loan/getFristPageProductList", "*"},
            {"/m2.0/facade/user/app/slideshow/query", "*"},
            {"/m2.0/facade/user/news/getnewsby/brandidandclassification/andpage", "*"},
            {"/m2.0/facade/user/thirdlevel/prod/list", "*"},
            {"/m2.0/facade/user/thirdlevel/prod/help/buy", "*"},
            {"/m2.0/facade/loan/getFristPageProductList", "*"},
            {"/m2.0/facade/user/brand/queryTel/id", "*"},
            {"/m2.0/facade/notice/jpush/history/brand", "*"},
            {"/m2.0/facade/user/app/slideshow/query", "*"},
            {"/m2.0/facade/credit/getBankPageList", "*"},
            {"/m2.0/facade/loan/getProductPageList", "*"},
            {"/m2.0/facade/loan/getProductPageList", "*"},
            {"/m2.0/facade/user/referees/detail ", "*"},
            {"/m2.0/facade/wechat/miniprogram/applogin", "*"},
            {"/m2.0/facade/wechat/applogin", "*"},
            {"/m2.0/facade/user/validateCode", "*"},
            {"/m2.0/facade/user/register", "*"},
            {"/m2.0/facade/user/smsCode", "*"},
            {"/m2.0/facade/user/app/forgetpassword/Verifypassword", "*"},
            {"/m2.0/facade/user/app/forgetpassword/sendmessage", "*"},
            {"/m2.0/facade/user/brand/query/id", "*"},
            {"/m2.0/facade/user/get/udun/realname/info", "*"},
            {"/m2.0/facade/user/bind/wx", "*"},
            {"/m2.0/facade/login/sms", "*"},
            {"/m2.0/facade/user/bank/default/upload", "*"},
            {"/m2.0/facade/user/bind/wx", "*"},
            {"/m2.0/facade/user/queryOneArticleById", "*"},
            {"/m2.0/facade/user/addBrowsingArticleRecord", "*"},
            {"/m2.0/facade/user/vestpackage/status", "*"},
            {"/m2.0/facade/user/vestpackage/status/modify", "*"},
            {"/m2.0/facade/addBrowsingArticleRecord", "*"},
            {"/m2.0/facade/payment/query/cardAppraisal", "*"},
            {"/m2.0/facade/user/queryPrizeConfig","*"},
            {"/m2.0/facade/user/queryResidueNum","*"},
            {"/m2.0/facade/user/thirdlevel/prod/list","*"},
            {"/m2.0/facade/user/luckyBigTurntable", "*"},
            {"/m2.0/facade/user/actDecrypt", "*"},
            {"/m2.0/facade/user/activityEncrypt", "*"},
            {"/m2.0/facade/user/queryInvitationRecord", "*"},
            {"/m2.0/facade/user/queryPurchaseHistory", "*"},
            {"/m2.0/facade/user/activity/status/query", "*"},
            {"/m2.0/facade/user/queryEventRecord", "*"},
            {"/m2.0/facade/Ai/findMySelfAidcard","*"}
//            {"/m2.0/facade/", "*"}
    };

    private static String[] deplay = null;

    @Value("#{'${ipaddress}'.split(',')}")
    public void setDeplay(String[] ipaddress) {
        this.deplay = ipaddress;
    }

    @Value(value = "${sso.logoutPath}")
    private String logoutPath;

    */
/**正在发版*//*

    public static final String IS_RELEASING = "1";

    */
/**项目是否在发版 1 : 正在发版    2 : 未发版 *//*

    @Value("${sso.isRelease}")
    private String isRelease;

    */
/**发版通知*//*

    @Value("${sso.releaseNotification}")
    private String releaseNotification;

//    @Value("${sso.redisAddress}")
//    private String ssoRedisAddress;

    @Override
    public void destroy() throws Exception {
        //JedisUtil.close();
    }

    private static final String HTTPS = "https";
    private static final int HTTPS_PORT = 443;

    @Override
    public void afterPropertiesSet() throws Exception {
        //JedisUtil.init(ssoRedisAddress);
    }

    @Override
    public String filterType() {
        return "pre";
    }

    @Override
    public int filterOrder() {
        return 1;
    }

    @Override
    public boolean shouldFilter() {
        RequestContext ctx = RequestContext.getCurrentContext();
        HttpServletRequest request = ctx.getRequest();
        try {
            request.setCharacterEncoding("UTF-8");
        } catch (Exception e) {
            log.info(e.getMessage());
        }
        //获取客户端请求路劲
        String uri = request.getRequestURI().toLowerCase();
        //获取客户端请求方法
        String method = request.getMethod();
        //获取客户端请求ip
        String requestIp = null;


        try {
            requestIp = HttpUtils.getIpAddress(request);
            log.info(String.format("====AuthenticationHeaderFilter.shouldFilter - %s", requestIp));
        } catch (IOException e) {
            log.info("Error=======================获取客户端ip异常!");
            e.printStackTrace();
        }

        */
/**URI带有 anon 表示为可匿名访问 则不过滤  例如通道的回调以及支付状态的页面接口*//*

        if(PatternMatchUtils.simpleMatch("*anon*", uri)){
            return false;
        }

        */
/**若项目在发版升级，则拒绝所有请求*//*

        if(StringUtils.equals(isRelease,IS_RELEASING)) {
            return true;
        }


        for (int j = 0; j < deplay.length; j++) {
            if (true || deplay[j].equals(requestIp)) {
                log.info("requst============ip" + requestIp);
                log.info(String.format("====AuthenticationHeaderFilter.shouldFilter - http method: (%s)", method));
                log.info(String.format("====AuthenticationHeaderFilter.shouldFilter - %s", uri));
                for (int i = 0; i < preAuthenticationIgnoreUris.length; i++) {
                    if (uri.startsWith(preAuthenticationIgnoreUris[i][0].toLowerCase()) &&
                            (preAuthenticationIgnoreUris[i][1].equals("*") || method.equalsIgnoreCase(preAuthenticationIgnoreUris[i][1]))) {
                        log.info("this will be not use filter uri={}", uri);
                        return false;
                    }
                }
            }
        }

        return true;

    }

    @Override
    public Object run() throws ZuulException {
        RequestContext requestContext = RequestContext.getCurrentContext();
        HttpServletRequest request = requestContext.getRequest();
        HttpServletResponse response = requestContext.getResponse();
        String uri = request.getRequestURI().toString().toLowerCase();
        String method = request.getMethod();
        //make url
        String servletPath = request.getServletPath();
        log.info(String.format("====AuthenticationHeaderFilter.run - %s request to %s", request.getMethod(), uri));

        */
/**若正在发版，则拒绝所有请求*//*

        if(StringUtils.equals(isRelease,IS_RELEASING)) {
            // response
            requestContext.setResponseStatusCode(HttpServletResponse.SC_UNAUTHORIZED);
            requestContext.setResponseBody("{\"code\":" + Conf.SSO_LOGIN_FAIL_RESULT.getCode() + ", \"msg\":\"" + releaseNotification + "\"}");
            requestContext.setSendZuulResponse(false);
            requestContext.set("isSuccess", false);// makes next filter see the status of this
            return null;
        }

        //sso begin

        // logout filter
        if (logoutPath != null
                && logoutPath.trim().length() > 0
                && logoutPath.equals(servletPath)) {

            // logout
            ssoTokenLoginHelper.logout(request);

            // response
            requestContext.setSendZuulResponse(false);
            requestContext.setResponseStatusCode(HttpServletResponse.SC_OK);
            requestContext.setResponseBody("{\"code\":" + ReturnT.SUCCESS_CODE + ", \"msg\":\"\"}");
            requestContext.set("isSuccess", false);
            return null;
        }

        // login filter
        log.info("===============================login filter 开始检查token==========================================");
//      SsoUser xxlUser = ssoTokenLoginHelper.loginCheck(request);
        String ssoToken = null;
        Enumeration<String> headerNames = request.getHeaderNames();
        while (headerNames.hasMoreElements()) {
            String name = headerNames.nextElement();
            if(Conf.SSO_SESSIONID.equalsIgnoreCase(name)) {  //xxl_sso_sessionid
                Enumeration<String> values = request.getHeaders(name);
                while (values.hasMoreElements()) {
                    ssoToken = values.nextElement();
                }
            }
        }
        SsoUser xxlUser = ssoTokenLoginHelper.loginCheckSessionId(ssoToken);
        if (xxlUser== null) {
            log.info("---------------------------token未查到User=================================");
            log.info("url has been filted url={}", request.getRequestURL());
            // response
            requestContext.setResponseStatusCode(HttpServletResponse.SC_UNAUTHORIZED);
            requestContext.setResponseBody("{\"code\":" + Conf.SSO_LOGIN_FAIL_RESULT.getCode() + ", \"msg\":\"" + Conf.SSO_LOGIN_FAIL_RESULT.getMsg() + "\"}");
            requestContext.setSendZuulResponse(false);
            requestContext.set("isSuccess", false);// makes next filter see the status of this
            return null;
        }

        // set sso user
        request.setAttribute(Conf.SSO_USER, xxlUser);

        requestContext.setResponseStatusCode(HttpStatus.OK.value());
        //requestContext.setResponseBody("Forbidden");
        requestContext.setSendZuulResponse(true);
        return null;
    }


    private void stopZuulRoutingWithError(RequestContext ctx, HttpStatus status, String responseText) {
        ctx.removeRouteHost();
        ctx.setResponseStatusCode(status.value());
        ctx.setResponseBody(responseText);
        ctx.setSendZuulResponse(false);
    }
}
*/
